﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Com.UmbracoChina
{
    public class UtilTool
    {
        /// <summary>
        /// 转换为短日期格式
        /// </summary>
        /// <param name="org"></param>
        /// <returns></returns>
        public static string ConvertShortTime(string org)
        {
            if (!string.IsNullOrEmpty(org))
            {
                return DateTime.Parse(org).ToString("yyyy-MM-dd HH:mm");
            }
            return "";
        }

        /// <summary>
        /// 转换数字
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int ConvertNumber(string source, int defaultValue)
        {
            if (!string.IsNullOrEmpty(source))
            {
                if (int.TryParse(source, out defaultValue))
                    return defaultValue;
            }
            return defaultValue;
        }

        /// <summary>
        /// 转换分页
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static int ConvertPageIndex(object collection)
        {
            if (collection != null)
            {
                return ConvertNumber(collection.ToString(), 1);
            }
            return 1;
        }

        /// <summary>
        /// 隐藏ip
        /// </summary>
        /// <param name="ip">原始ip</param>
        /// <returns></returns>
        public static string HideIp(string ip)
        {
            string[] ips = ip.Split('.');
            ips[1] = "*";
            ips[2] = "*";
            return string.Join(".", ips);
        }

        /// <summary>
        /// 隐藏email
        /// </summary>
        /// <param name="email">原始email</param>
        /// <returns></returns>
        public static string hideEmail(string email)
        {
            return "***" + email.Substring(email.IndexOf("@"));
        }
    }
}